clear
clear matrix
clear mata
set matsize 4000
set maxvar 10000

*Note: first change working directory replication package folder

run "scripts/programs/program_data.do"
run "scripts/programs//program_regs.do"
run "scripts/programs/program_regtable.do"

/*********************************************************************************/
*Set program parameters
local radius = 20
local angle = 15
local length = 1
/*********************************************************************************/

data anypop pop anyhu hu hudens lnpop popdens inc_pcincome inc_totinc inc_huxpcinc inc_pct_poverty ///
	count_res count_mobile count_sfr ///
	any_res val_res val_mobile val_sfr count_vac_hu count_nonvac_hu ///
	avgval_res avgval_mobile avgval_sfr ///
	race_pct_whitenh race_pct_amind ///
	race_pct_black race_pct_hisp tenure_pct_rent vac_pct_vacation tri  ///
	lnt lnint t1 t2 t3 t4 t5 t6 tmissing roadpcttot majorroad tritot habpolytot hablinetot wsimptot ///
	campground wilderness ///
	using "data/`radius'-by-`length'km_`angle'deg/data_centroid.dta", ///
	maxlags(0) maxleads(0) burnt(centroid) tcuts(1,2,3,4,5,8)

drop if fire_size < 1000

/*************************	SPEC TABLE - Assessors ******************************/

gen x = (count_res != .)
bysort firenum: egen assessor_data = sum(x)
replace assessor_data = (assessor_data > 0)
drop x

replace race_pct_whitenh = 0 if count_res == 0
replace inc_pct_poverty = 0 if count_res == 0
replace inc_pcincome = 0 if count_res== 0
replace tenure_pct_rent = 0 if count_res == 0

local huvars any_res count_res avgval_res val_res
local indepvars habpolytot wsimptot campground wilderness tritot roadpcttot
local firevars lnt tmissing lnint wind_diff majorroad
local demvars `huvars' `indepvars'

set matsize 4000
local link cloglog
 regs y `demvars' `firevars' if complexfire != 1 & year > 2011, link(`link') cluster(firenum) save(fe) fe(firenum)
capture gen sample = e(sample)

preserve
keep firenum sample
collapse (mean) sample, by(firenum)
replace sample = 1 if sample > 0
save "data/`radius'-by-`length'km_`angle'deg/sample_assessors.dta", replace
restore

regs y `demvars' `firevars' if sample == 1 & complexfire != 1 & year > 2011, link(`link') cluster(firenum) save(both)
regs y `demvars' `firevars' campground wilderness if sample == 1 & complexfire != 1 & year > 2011, link(`link') cluster(firenum) save(both)
regs y `demvars' if sample == 1 & complexfire != 1 & year > 2011, link(`link') cluster(firenum) save(fe_base) fe(firenum)
regs y `firevars' if sample == 1 & complexfire != 1 & year > 2011, link(`link') cluster(firenum) save(fe_fire) fe(firenum)
regs y `demvars' `firevars' if sample == 1 & complexfire != 1 & year > 2011, link(logit) cluster(firenum) save(logit) fe(firenum)
regs y `demvars' `firevars' if sample == 1 & complexfire != 1 & year > 2011, link(probit) cluster(firenum) save(probit) fe(firenum)
regs y `demvars' `firevars' if sample == 1 & complexfire != 1 & year > 2011, link(lpm) cluster(firenum) save(lpm) fe(firenum)


local dir "Results\August_2021\Spec_Table_Assessor\"
capture mkdir `dir'
noi regtable fe_base fe_fire both fe using "`dir'table_contents.tex" ///
 	, keep(`demvars' `firevars') tex

local dir "Results\August_2021\Appendix_Table_Assessor\"
capture mkdir `dir'
noi regtable fe logit probit lpm using "`dir'table_contents.tex" ///
 	, keep(`demvars' `firevars') tex


/*************************	SPEC TABLE - Census ******************************/


/*********************************************************************************/
*Set program parameters
local radius = 20
local angle = 15
local length = 1
/*********************************************************************************/

capture drop sample

replace race_pct_whitenh = 0 if count_res == 0
replace inc_pct_poverty = 0 if count_res == 0
replace inc_pcincome = 0 if count_res== 0
replace tenure_pct_rent = 0 if count_res == 0

local huvars anyhu hu inc_pcincome huxpcinc
local indepvars habpolytot wsimptot campground wilderness tritot roadpcttot
local firevars lnt tmissing lnint wind_diff majorroad
local demvars `huvars' `indepvars'

set matsize 4000
local link cloglog
regs y `demvars' `firevars' if complexfire != 1, link(`link') cluster(firenum) save(fe) fe(firenum)
capture gen sample = e(sample)

preserve
keep firenum sample
collapse (mean) sample, by(firenum)
replace sample = 1 if sample > 0
save "data/`radius'-by-`length'km_`angle'deg/sample_census.dta", replace
restore

regs y `demvars' `firevars' if sample == 1 & complexfire != 1, link(`link') cluster(firenum) save(both)
regs y `demvars' if sample == 1 & complexfire != 1, link(`link') cluster(firenum) save(fe_base) fe(firenum)
regs y `firevars' if sample == 1 & complexfire != 1, link(`link') cluster(firenum) save(fe_fire) fe(firenum)
regs y `demvars' `firevars' if sample == 1 & complexfire != 1, link(logit) cluster(firenum) save(logit) fe(firenum)
regs y `demvars' `firevars' if sample == 1 & complexfire != 1, link(probit) cluster(firenum) save(probit) fe(firenum)
regs y `demvars' `firevars' if sample == 1 & complexfire != 1, link(lpm) cluster(firenum) save(lpm) fe(firenum)


local dir "Results/Spec_Table_Census/"
capture mkdir `dir'
noi regtable fe_base fe_fire both fe using "`dir'table_contents.tex" ///
 	, keep(`demvars' `firevars') tex

local dir "Results/Appendix_Table_Census/"
capture mkdir `dir'
noi regtable fe logit probit lpm using "`dir'table_contents.tex" ///
 	, keep(`demvars' `firevars') tex
